package com.jade.store.wechat.service.impl;

import com.jade.bss.base.customer.Customer;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.store.wechat.dao.WechatAppDao;
import com.jade.store.wechat.entity.WechatApp;
import com.jade.store.wechat.service.WechatAppFacade;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created with IntelliJ IDEA.
 * User: JinYa 2009_jinya@163.com
 * Date: 2016-5-5
 * Time: 10:53
 * Version : 1.0
 * Remark:
 * History:
 */
@Service("WechatAppFacade")
public class WechatAppFacadeImpl extends BaseFacadeImpl implements WechatAppFacade {
    @Inject
    @Named("WechatAppDao")
    private WechatAppDao wechatAppDao;
    public WechatApp getLatestWechatAppByAppID(String appid) throws Exception {
        Criteria criteria = new Criteria();
        criteria.add(Restrictions.eq("authorizer_appid", appid));
        criteria.addOrder(com.jade.framework.base.criterion.Order.desc("createTime"));
        PagingList<WechatApp> list = wechatAppDao.queryEntity(WechatApp.class, criteria);
        if(list!=null && list.size()>0)return list.get(0);
        return null;
    }

    public WechatApp getDefaultApp(Customer customer) throws Exception {
        Criteria criteria = new Criteria();
        criteria.add(Restrictions.eq("ownerId", customer.getOwnerId()));
        criteria.add(Restrictions.eq("status", WechatApp.STATUS_NORMAL));
        criteria.addOrder(com.jade.framework.base.criterion.Order.desc("createTime"));
        PagingList<WechatApp> list = wechatAppDao.queryEntity(WechatApp.class, criteria);
        if(list!=null && list.size()>0)return list.get(0);
        return null;
    }
}
